﻿Imports TMySQL
Public Class frmGlobalPrivileges
    Private _Privs As TMyGlobalPrivileges

    Private Sub New()
        ' この呼び出しは、Windows フォーム デザイナで必要です。
        InitializeComponent()
        ' InitializeComponent() 呼び出しの後で初期化を追加します。
    End Sub

    Public Sub New(ByVal AGlobalPrivileges As TMyGlobalPrivileges)
        Me.New()
        _Privs = AGlobalPrivileges
        InitFrm()
    End Sub

    Private Sub InitFrm()
        frmMain.GeneralSettings.ApplyAll(Me)
        _Privs.Refresh()
        txtUserName.Text = _Privs.UserName
        txtHostName.Text = _Privs.HostName
        txtPassword1.Text = _Privs.Password
        txtPassword2.Text = _Privs.Password

        chkSelect.Checked = _Privs.SelectPriv
        chkInsert.Checked = _Privs.InsertPriv
        chkUpdate.Checked = _Privs.UpdatePriv
        chkDelete.Checked = _Privs.DeletePriv
        chkFile.Checked = _Privs.FilePriv

        chkCreate.Checked = _Privs.CreatePriv
        chkAlter.Checked = _Privs.AlterPriv
        chkIndex.Checked = _Privs.IndexPriv
        chkDrop.Checked = _Privs.DropPriv
        chkCreateTemporaryTables.Checked = _Privs.CreateTmpTablePriv
        chkShowView.Checked = _Privs.ShowViewPriv
        chkCreateRoutine.Checked = _Privs.CreateRoutinePriv
        chkAlterRoutine.Checked = _Privs.AlterRoutinePriv
        chkExecute.Checked = _Privs.ExecutePriv
        chkCreateView.Checked = _Privs.CreateViewPriv
        chkEvent.Checked = _Privs.EventPriv
        chkTrigger.Checked = _Privs.TriggerPriv

        chkGrant.Checked = _Privs.GrantPriv
        chkSuper.Checked = _Privs.SuperPriv
        chkProcess.Checked = _Privs.ProcessPriv
        chkReload.Checked = _Privs.ReloadPriv
        chkShutdown.Checked = _Privs.ShutdownPriv
        chkShowDatabase.Checked = _Privs.ShowDbPriv
        chkLockTables.Checked = _Privs.LockTablesPriv
        chkReferences.Checked = _Privs.ReferencesPriv
        chkReplClient.Checked = _Privs.ReplClientPriv
        chkReplSlave.Checked = _Privs.ReplSlavePriv
        chkCreateUser.Checked = _Privs.CreateUserPriv

        txtMaxConnections.Text = _Privs.MaxConnections.ToString
        txtMaxQueries.Text = _Privs.MaxQuestions.ToString
        txtMaxUpdates.Text = _Privs.MaxUpdates.ToString
        txtMaxUserConnections.Text = _Privs.MaxUserConnections.ToString
    End Sub

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        InitFrm()
    End Sub

    Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click
        Try
            Dim changed As Boolean = False
            CheckEntries(changed)
            If changed Then
                If MsgBox("権限を変更しますか？", MsgBoxStyle.Question Or MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                    ChangePrivs()
                    Call MsgBox("権限を変更しました", MsgBoxStyle.Information)
                End If
            End If
        Catch ex As Exception
            Call MsgBox(ex.Message, MsgBoxStyle.Exclamation)
        End Try
    End Sub

    Private Sub ChangePrivs()
        Dim myQuery As New TMyQuery
        Try
            myQuery.Session = frmMain.Connection
            RevokeAll(myQuery)
            AddPrivs(myQuery)
            InitFrm()
        Finally
            myQuery.Dispose()
        End Try
    End Sub

    Private Sub RevokeAll(ByRef AQuery As TMyQuery)
        AQuery.Clear()
        AQuery.SQL.Append("REVOKE ALL PRIVILEGES ON * . * FROM '").Append(_Privs.UserName).Append("'@'").Append(_Privs.HostName).Append("'")
        AQuery.Execute()
        AQuery.Clear()
        AQuery.SQL.Append("REVOKE GRANT OPTION ON * . * FROM '").Append(_Privs.UserName).Append("'@'").Append(_Privs.HostName).Append("'")
        AQuery.Execute()
    End Sub

    Private Sub AddPrivs(ByRef AQuery As TMyQuery)

        Dim sb As New System.Text.StringBuilder
        Dim sbSQL As New System.Text.StringBuilder
        sbSQL.AppendLine("GRANT ")

        If chkSelect.Checked Then sb.AppendLine("SELECT")
        If chkInsert.Checked Then sb.AppendLine("INSERT")
        If chkUpdate.Checked Then sb.AppendLine("UPDATE")
        If chkDelete.Checked Then sb.AppendLine("DELETE")
        If chkFile.Checked Then sb.AppendLine("FILE")

        If chkCreate.Checked Then sb.AppendLine("CREATE")
        If chkAlter.Checked Then sb.AppendLine("ALTER")
        If chkIndex.Checked Then sb.AppendLine("INDEX")
        If chkDrop.Checked Then sb.AppendLine("DROP")
        If chkCreateTemporaryTables.Checked Then sb.AppendLine("CREATE TEMPORARY TABLES")
        If chkShowView.Checked Then sb.AppendLine("SHOW VIEW")
        If chkCreateRoutine.Checked Then sb.AppendLine("CREATE ROUTINE")
        If chkAlterRoutine.Checked Then sb.AppendLine("ALTER ROUTINE")
        If chkExecute.Checked Then sb.AppendLine("EXECUTE")
        If chkCreateView.Checked Then sb.AppendLine("CREATE VIEW")
        If chkEvent.Checked Then sb.AppendLine("EVENT")
        If chkTrigger.Checked Then sb.AppendLine("TRIGGER")

        If chkSuper.Checked Then sb.AppendLine("SUPER")
        If chkProcess.Checked Then sb.AppendLine("PROCESS")
        If chkReload.Checked Then sb.AppendLine("RELOAD")
        If chkShutdown.Checked Then sb.AppendLine("SHUTDOWN")
        If chkShowDatabase.Checked Then sb.AppendLine("SHOW DATABASES")
        If chkLockTables.Checked Then sb.AppendLine("LOCK TABLES")
        If chkReferences.Checked Then sb.AppendLine("REFERENCES")
        If chkReplClient.Checked Then sb.AppendLine("REPLICATION CLIENT")
        If chkReplSlave.Checked Then sb.AppendLine("REPLICATION SLAVE")
        If chkCreateUser.Checked Then sb.AppendLine("CREATE USER")
        Dim sTmp As String = sb.ToString.Replace(vbNewLine, ",")
        If sTmp.Length > 0 Then
            sTmp = Common.Left(sTmp, sTmp.Length - 1)
        End If
        sbSQL.AppendLine(sTmp)
        sbSQL.AppendLine(" ON *.*")
        sbSQL.Append("TO '").Append(_Privs.UserName).Append("'@'").Append(_Privs.HostName).AppendLine("'")
        If chkGrant.Checked Then
            sbSQL.AppendLine("WITH GRANT OPTION")
        End If
        If Not _Privs.Password.Equals(txtPassword1.Text) AndAlso txtPassword1.Text.Equals(txtPassword2.Text) Then
            sbSQL.Append("IDENTIFIED BY '").AppendLine(txtPassword1.Text.Replace("'", "''"))
        End If
        sbSQL.Append("MAX_QUERIES_PER_HOUR ").AppendLine(Common.StrToIntDef(txtMaxQueries.Text, 0))
        sbSQL.Append("MAX_CONNECTIONS_PER_HOUR ").AppendLine(Common.StrToIntDef(txtMaxConnections.Text, 0))
        sbSQL.Append("MAX_UPDATES_PER_HOUR ").AppendLine(Common.StrToIntDef(txtMaxUpdates.Text, 0))
        sbSQL.Append("MAX_USER_CONNECTIONS ").AppendLine(Common.StrToIntDef(txtMaxUserConnections.Text, 0))
        AQuery.Clear()
        AQuery.SQL.Append(sbSQL.ToString)
        AQuery.Execute()
    End Sub

    Private Sub CheckEntries(ByRef AChanged As Boolean)
        AChanged = False
        If Not txtPassword1.Text.Equals(txtPassword2.Text) Then
            Throw New Exception
        End If

        If Not chkSelect.Checked.Equals(_Privs.SelectPriv) Then AChanged = True
        If Not chkInsert.Checked.Equals(_Privs.InsertPriv) Then AChanged = True
        If Not chkUpdate.Checked.Equals(_Privs.UpdatePriv) Then AChanged = True
        If Not chkDelete.Checked.Equals(_Privs.DeletePriv) Then AChanged = True
        If Not chkFile.Checked.Equals(_Privs.FilePriv) Then AChanged = True

        If Not chkCreate.Checked.Equals(_Privs.CreatePriv) Then AChanged = True
        If Not chkAlter.Checked.Equals(_Privs.AlterPriv) Then AChanged = True
        If Not chkIndex.Checked.Equals(_Privs.IndexPriv) Then AChanged = True
        If Not chkDrop.Checked.Equals(_Privs.DropPriv) Then AChanged = True
        If Not chkCreateTemporaryTables.Checked.Equals(_Privs.CreateTmpTablePriv) Then AChanged = True
        If Not chkShowView.Checked.Equals(_Privs.ShowViewPriv) Then AChanged = True
        If Not chkCreateRoutine.Checked.Equals(_Privs.CreateRoutinePriv) Then AChanged = True
        If Not chkAlterRoutine.Checked.Equals(_Privs.AlterRoutinePriv) Then AChanged = True
        If Not chkExecute.Checked.Equals(_Privs.ExecutePriv) Then AChanged = True
        If Not chkCreateView.Checked.Equals(_Privs.CreateViewPriv) Then AChanged = True
        If Not chkEvent.Checked.Equals(_Privs.EventPriv) Then AChanged = True
        If Not chkTrigger.Checked.Equals(_Privs.TriggerPriv) Then AChanged = True

        If Not chkGrant.Checked.Equals(_Privs.GrantPriv) Then AChanged = True
        If Not chkSuper.Checked.Equals(_Privs.SuperPriv) Then AChanged = True
        If Not chkProcess.Checked.Equals(_Privs.ProcessPriv) Then AChanged = True
        If Not chkReload.Checked.Equals(_Privs.ReloadPriv) Then AChanged = True
        If Not chkShutdown.Checked.Equals(_Privs.ShutdownPriv) Then AChanged = True
        If Not chkShowDatabase.Checked.Equals(_Privs.ShowDbPriv) Then AChanged = True
        If Not chkLockTables.Checked.Equals(_Privs.LockTablesPriv) Then AChanged = True
        If Not chkReferences.Checked.Equals(_Privs.ReferencesPriv) Then AChanged = True
        If Not chkReplClient.Checked.Equals(_Privs.ReplClientPriv) Then AChanged = True
        If Not chkReplSlave.Checked.Equals(_Privs.ReplSlavePriv) Then AChanged = True
        If Not chkCreateUser.Checked.Equals(_Privs.CreateUserPriv) Then AChanged = True

        If Not Common.StrToIntDef(txtMaxConnections.Text, 0).Equals(_Privs.MaxConnections) Then AChanged = True
        If Not Common.StrToIntDef(txtMaxQueries.Text, 0).Equals(_Privs.MaxQuestions) Then AChanged = True
        If Not Common.StrToIntDef(txtMaxUpdates.Text, 0).Equals(_Privs.MaxUpdates) Then AChanged = True
        If Not Common.StrToIntDef(txtMaxUserConnections.Text, 0).Equals(_Privs.MaxUserConnections) Then AChanged = True

    End Sub

    Private Sub txt_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtMaxUserConnections.KeyPress, txtMaxUpdates.KeyPress, txtMaxQueries.KeyPress, txtMaxConnections.KeyPress
        If e.KeyChar < "0"c OrElse e.KeyChar > "9"c Then
            e.Handled = True
        End If
    End Sub


    Private Sub cmdCheckOnAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCheckOnAll.Click
        AllCheck(True)
    End Sub

    Private Sub cmdCheckOffAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCheckOffAll.Click
        AllCheck(False)
    End Sub

    Private Sub AllCheck(ByVal AChecked As Boolean)
        chkSelect.Checked = AChecked
        chkInsert.Checked = AChecked
        chkUpdate.Checked = AChecked
        chkDelete.Checked = AChecked
        chkFile.Checked = AChecked

        chkCreate.Checked = AChecked
        chkAlter.Checked = AChecked
        chkIndex.Checked = AChecked
        chkDrop.Checked = AChecked
        chkCreateTemporaryTables.Checked = AChecked
        chkShowView.Checked = AChecked
        chkCreateRoutine.Checked = AChecked
        chkAlterRoutine.Checked = AChecked
        chkExecute.Checked = AChecked
        chkCreateView.Checked = AChecked
        chkEvent.Checked = AChecked
        chkTrigger.Checked = AChecked

        chkSuper.Checked = AChecked
        chkProcess.Checked = AChecked
        chkReload.Checked = AChecked
        chkShutdown.Checked = AChecked
        chkShowDatabase.Checked = AChecked
        chkLockTables.Checked = AChecked
        chkReferences.Checked = AChecked
        chkReplClient.Checked = AChecked
        chkReplSlave.Checked = AChecked
        chkCreateUser.Checked = AChecked
    End Sub
End Class